package com.qianguo.shop.fragment.tab;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.eva.androidlibrary.widget.CircleImageView;
import com.qianguo.shop.R;
import com.qianguo.shop.activity.IMMessageActivity;
import com.qianguo.shop.base.App;
import com.qianguo.shop.base.BaseFragment;
import com.qianguo.shop.entity.Contact;
import com.qianguo.shop.entity.Contacts;
import com.qianguo.shop.entity.mock.MockContacts;
import com.eva.androidlibrary.widget.SwipeRefreshRecyclerView;

import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * 商家消息Fragment
 *
 * @author GUXIA318
 * @date 2015-09-16 16:23
 */
@EFragment(R.layout.fragment_tab_im_contacts)
public class IMContactsTabFragment extends BaseFragment {

    private static final int MESSAGE_TYPE_ME = 1;
    private static final int MESSAGE_TYPE_OTHER = 2;

    @ViewById(R.id.im_contacts)
    SwipeRefreshRecyclerView mRycImMessageList;
    ContactsAdapter mAdapter = null;

    private Contacts mContacts = null;

    @Override
    protected void initializeUI() {
        mAdapter = new ContactsAdapter(getActivity());
        mRycImMessageList.setAdapter(mAdapter);
        mRycImMessageList.setOnItemClickListener(new SwipeRefreshRecyclerView.OnRecyclerItemClickListener() {
            @Override
            public void onClick(int position) {
                Contact contact = mContacts.getItemAt(position);
                IMMessageActivity.jumpToIMMessageActivity(getActivity(),
                        contact.getContactId(), contact.getUserId(), contact.getName());
            }
        });

        mContacts = new MockContacts();
        mAdapter.notifyDataSetChanged();
    }

    private class ContactsAdapter extends RecyclerView.Adapter<ContactViewHolder> {

        private Context mContext = null;
        private LayoutInflater mInflater = null;

        ContactsAdapter(Context context) {
            mContext = context;
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ContactViewHolder(mInflater.inflate(R.layout.item_im_contact, parent, false));
        }

        @Override
        public void onBindViewHolder(ContactViewHolder holder, int position) {
            holder.onBind(mContacts.getItemAt(position));
        }

        @Override
        public int getItemCount() {
            return mContacts != null ? mContacts.getItemCount() : 0;
        }
    }

    private static class ContactViewHolder extends RecyclerView.ViewHolder {

        private CircleImageView imgAvatar;
        private TextView txvUsername;
        private TextView txvTime;
        private TextView txvLastMessage;
        private TextView txvLastOrder;
        private TextView txvLastStatus;
        private Contact contact;
        private App app;

        public ContactViewHolder(View itemView) {
            super(itemView);
            imgAvatar = (CircleImageView) itemView.findViewById(R.id.im_avater);
            txvUsername = (TextView) itemView.findViewById(R.id.im_username);
            txvTime = (TextView) itemView.findViewById(R.id.im_last_time);
            txvLastMessage = (TextView) itemView.findViewById(R.id.im_last_message);
            txvLastOrder = (TextView) itemView.findViewById(R.id.im_last_order_code);
            txvLastStatus = (TextView) itemView.findViewById(R.id.im_last_status);
            app = (App) itemView.getContext().getApplicationContext();
        }

        void onBind(Contact contact) {
            this.contact = contact;
            app.getGlobalImageLoader().loadImage(
                    itemView.getContext(), imgAvatar, contact.getAvatarUrl());
            txvUsername.setText(this.contact.getName());
            txvTime.setText(this.contact.getTime());
            txvLastMessage.setText(this.contact.getLastMessage());
            txvLastOrder.setText(this.contact.getLastOrderCode());
            txvLastStatus.setText(this.contact.getLastOrderStatus());
        }
    }
}
